Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
no-unsafe-optional-chaining does not work with "||" or condition

no-unsafe-optional-chaining is an amazing eslint rule, which help us identify why we are doing wrong with optional chaining..

I have one case where i need to perform a sort operation on id (in numbers) which could be in string type which make sense to throw error because the it could result in NaN if order is not a number

  const sortComparer = (a, b) => (+a?.order) - (+b?.order)

However, if i do handle no-unsafe-optional-chaining as follow it still throws error and does not work:

  const sortComparer = (a, b) => (+a.order || 0) - (+b.order || 0),

The only way to make it work is like this:

  const sortComparer = (a, b) => (a?.order ? +a.order : 0) - (b?.order ? +b.order : 0),

Does anyone know how to fix this in a better way to avoid this issue ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This should work:

sortComparer: (a, b) => +(a?.order || 0) - +(b?.order || 0),

Move the + out of the (), and use the optional chaining to handle a or b being null.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!